<?php

/**
 * 
 *系统使用
 */

namespace app\api\controller\basemanage;
use Think\Db;
use app\api\controller\common\ApiBase;
use think\Controller; // den
class Space extends ApiBase
{

    /**
     *
     * @return \think\response\Json|void
     *  comppub
     *
     */

    public function getList(){
        $parent=db('server_cate')->where(['is_delete'=>0,'parent_id'=>0])->order('sort desc')->select();
        $list=db('server_cate')->where(['is_delete'=>0,'parent_id'=>array('gt',0)])->select();
        foreach ($parent as &$v){
            $v['children']=[];
            foreach ($list as &$vv){
                if($vv['parent_id']==$v['id']){
                    $v['children'][]=$vv;
                }
            }
        }
        return json(['code'=>1,'msg'=>'success','data'=>$parent]);
    }

    public function getInfo(){
//        $parent=db('server_cate')->where(['is_delete'=>0,'parent_id'=>0])->order('sort desc')->select();
//        $list=db('server_cate')->where(['is_delete'=>0,'parent_id'=>array('gt',0)])->select();
//        foreach ($parent as &$v){
//            $v['children']=[];
//            foreach ($list as &$vv){
//                if($vv['parent_id']==$v['id']){
//                    $v['children'][]=$vv;
//                }
//            }
//        }
        $parent=array();
        $parent['filetypearr']=array('图片','视频','其他','空闲');
        $parent['picusepre']=10;
        $parent['picusenum']=24;
        $parent['picusespace']='1.0t';



        $parent['videousepre']=40;
        $parent['videousenum']=30;
        $parent['videousespace']='10t';

        $parent['nousepre']=45;
        $parent['nousenum']=30;
        $parent['nousespace']='10t';

        $parent['otherpre']=5;
        $parent['othernum']=30;
        $parent['otherspace']='10t';

        return json(['code'=>1,'msg'=>'success','data'=>$parent]);
    }


    /**
     * 添加
     * @return \think\response\Json|void
     *  zs_question       zs_question_option
     *  zs_question_type
     */
    public function add(){
        $data['create_time']=time();
        $data['update_time']=time();
        $data['title']=$this->request->param('title');   // 1单选 2多选3  填空
        $data['sort']=$this->request->param('sort');   // 1单选 2多选3  填空
        $data['status']=$this->request->param('status');
        $data['iswarn']=$this->request->param('iswarn');
//        $data['page_type']=$this->request->param('page_type');
        $parent_id=$this->request->param('parent_id');
        $data['parent_id']=$parent_id?$parent_id:0;
        $data['level']=$parent_id?1:0;
        $res=db('server_cate')->insert($data);
        if(!$res){
            return json(['code'=>0,'msg'=>'操作失败','data'=>'']);
        }
        return json(['code'=>1,'msg'=>'操作成功','data'=>'']);
    }

    /**
     * 删除
     */
    public function del(){
        $id = $this->request->param('id');
        $r = db('server_cate')->where(['id'=>$id,'is_delete'=>0])->find();
        if(empty($r)){
            return json(['code'=>0,'msg'=>'参数有误','data'=>'']);
        }
        $r = db('server_cate')->where(['parent_id'=>$id,'is_delete'=>0])->find();
        if(!empty($r)){
            return json(['code'=>0,'msg'=>'此数据包含子项目','data'=>'']);
        }
        $r = db('server_cate')->where(['id'=>$id])->update(['is_delete'=>1]);
        if(!$r){
            return json(['code'=>0,'msg'=>'操作失败']);
        }
        return json(['code'=>1,'msg'=>'操作成功']);

    }


    /**
     * 基本信息修改
     * @return \think\response\Json|void
     */
    public function edit()
    {
        $id = $this->request->param('id');
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '参数有误，请核查']);
        }
        $data['update_time']=time();
        $data['title']=$this->request->param('title');   // 1单选 2多选3  填空
        $data['sort']=$this->request->param('sort');   // 1单选 2多选3  填空
        $data['status']=$this->request->param('status');
        $data['iswarn']=$this->request->param('iswarn');
        $parent_id=$this->request->param('parent_id');
//        $data['page_type']=$this->request->param('page_type');
        $data['parent_id']=$parent_id?$parent_id:0;
        $data['level']=$parent_id?1:0;
//        var_dump( $data);die;
        $where['id'] = $id;
        $r=db('server_cate')->where($where)->update($data);
        if (!$r) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }
        return json(['code' => 1, 'msg' => '操作成功']);
    }

    /**
     * 基本信息修改
     * @return \think\response\Json|void
     */
    public function getDetail()
    {
        $id = $this->request->param('id');
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '参数有误，请核查']);
        }
        $where['id'] = $id;
        $r=db('technician')->where($where)->find();
        if (!$r) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }

        return json(['code' => 1, 'msg' => '操作成功','data'=>$r]);
    }


}
